/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import componenten.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.Locatie;
import model.Toernooi;

/**
 *
 * @author Gebruiker
 */
public class ToernooiAanmaken extends javax.swing.JFrame {

    private Toernooi clsToernooi;

    /**
     * Creates new form ToernooiAanmaken
     */
    public ToernooiAanmaken() {

        initComponents();

        Toernooi toernooi = new Toernooi();
        this.clsToernooi = toernooi;

        this.txtNaam.setText(this.clsToernooi.getNaam());
        this.txtDatum.setText(this.clsToernooi.getDatum());
        this.txtTijd.setText(this.clsToernooi.getTijd());
        this.txtInleggeld.setText("" + this.clsToernooi.getInleggeld());
        this.txtMinAantalSpelers.setText("" + this.clsToernooi.getMinAantalSpelers());
        this.txtHuisnummer.setText(this.clsToernooi.getHuisnummer());
        this.txtPostcode.setText(this.clsToernooi.getPostcode());

        this.btnToernooiToevoegen.setText("Toernooi Toevoegen");
        this.vulTabelLocatie();
    }

    private void vulTabelLocatie() {

        String[] kolommen = {"Naam", "Plaats", "Straat", "Postcode", "Huisnummer"};
        DefaultTableModel model = new DefaultTableModel(kolommen, 0);

        try {
            String sql = "SELECT * FROM locatie";
            ResultSet resultaat = Database.executeSelectQuery(sql);

            while (resultaat.next()) {
                String huisnummer = resultaat.getString("locatie_huisnummer");
                String postcode = resultaat.getString("locatie_postcode");

                Locatie tabelVulling = new Locatie(huisnummer, postcode);
                model.addRow(tabelVulling.getInformatie());
            }
        } catch (SQLException e) {
            System.out.print(e);
        }

        tblLocatie.setModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblHuisnummer = new javax.swing.JLabel();
        lblPostcode = new javax.swing.JLabel();
        lblDatum = new javax.swing.JLabel();
        lblTijd = new javax.swing.JLabel();
        lblInleg = new javax.swing.JLabel();
        lblNaam = new javax.swing.JLabel();
        lblDeelnemers = new javax.swing.JLabel();
        txtNaam = new javax.swing.JTextField();
        txtDatum = new javax.swing.JTextField();
        txtTijd = new javax.swing.JTextField();
        txtInleggeld = new javax.swing.JTextField();
        txtHuisnummer = new javax.swing.JTextField();
        txtPostcode = new javax.swing.JTextField();
        txtMinAantalSpelers = new javax.swing.JTextField();
        btnToernooiToevoegen = new javax.swing.JButton();
        jScrollPane = new javax.swing.JScrollPane();
        tblLocatie = new javax.swing.JTable();
        Cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Toernooi Aanmaken");
        setMinimumSize(null);

        lblHuisnummer.setText("Huisnummer");

        lblPostcode.setText("postcode");

        lblDatum.setText("Datum");

        lblTijd.setText("Tijd");

        lblInleg.setText("Inleg");

        lblNaam.setText("Naam");

        lblDeelnemers.setText("min deelnemers");

        txtDatum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDatumActionPerformed(evt);
            }
        });

        txtHuisnummer.setFocusable(false);

        txtPostcode.setFocusable(false);

        btnToernooiToevoegen.setText("Toevoegen");
        btnToernooiToevoegen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToernooiToevoegenActionPerformed(evt);
            }
        });

        tblLocatie.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblLocatie.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblLocatieMouseClicked(evt);
            }
        });
        jScrollPane.setViewportView(tblLocatie);

        Cancel.setText("cancel");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDatum, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(btnToernooiToevoegen, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblDeelnemers, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                                .addComponent(lblHuisnummer, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(lblTijd, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblInleg, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPostcode, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(lblNaam)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTijd, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                            .addComponent(txtInleggeld)
                            .addComponent(txtMinAantalSpelers)
                            .addComponent(txtPostcode)
                            .addComponent(txtHuisnummer)
                            .addComponent(txtDatum)
                            .addComponent(txtNaam))
                        .addGap(20, 20, 20))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Cancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)))
                .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
                .addGap(15, 15, 15))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNaam, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNaam))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblDatum)
                            .addComponent(txtDatum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTijd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblTijd))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtInleggeld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblInleg))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtMinAantalSpelers, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDeelnemers))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPostcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPostcode))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtHuisnummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblHuisnummer))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnToernooiToevoegen)
                            .addComponent(Cancel))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

        private void btnToernooiToevoegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToernooiToevoegenActionPerformed

            clsToernooi.setNaam(txtNaam.getText());
            clsToernooi.setDatum(txtDatum.getText());
            clsToernooi.setTijd(txtTijd.getText());
            clsToernooi.setInleggeld(txtInleggeld.getText());
            clsToernooi.setMinAantalSpelers(txtMinAantalSpelers.getText());
            clsToernooi.setPostcode(txtPostcode.getText());
            clsToernooi.setHuisnummer(txtHuisnummer.getText());

            clsToernooi.toevoegen();

            if (clsToernooi.isGevalideerd()) {
                this.setVisible(false);
                
            } else {
                String bericht = "";
                ArrayList berichten = clsToernooi.getBericht();
                for (int i = 0; i < berichten.size(); i++) {
                    bericht = bericht + (String) berichten.get(i) + "\n";
                }

                JOptionPane.showMessageDialog(null, bericht);
                clsToernooi.resetBericht();
            }

//            if (clsToernooi.insert()) {
//                this.dispose();
//            } else {
//                System.out.println("Persoon toevoegen: opslaan: " + "error bij opslaan");
//            }

    }//GEN-LAST:event_btnToernooiToevoegenActionPerformed

    private void tblLocatieMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblLocatieMouseClicked
        int rij = tblLocatie.getSelectedRow();
        txtHuisnummer.setText((String) tblLocatie.getModel().getValueAt(rij, 3));
        txtPostcode.setText((String) tblLocatie.getModel().getValueAt(rij, 4));
    }//GEN-LAST:event_tblLocatieMouseClicked

    private void txtDatumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDatumActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDatumActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
       this.dispose();
    }//GEN-LAST:event_CancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ToernooiAanmaken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ToernooiAanmaken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ToernooiAanmaken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ToernooiAanmaken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ToernooiAanmaken().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancel;
    private javax.swing.JButton btnToernooiToevoegen;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JLabel lblDatum;
    private javax.swing.JLabel lblDeelnemers;
    private javax.swing.JLabel lblHuisnummer;
    private javax.swing.JLabel lblInleg;
    private javax.swing.JLabel lblNaam;
    private javax.swing.JLabel lblPostcode;
    private javax.swing.JLabel lblTijd;
    private javax.swing.JTable tblLocatie;
    private javax.swing.JTextField txtDatum;
    private javax.swing.JTextField txtHuisnummer;
    private javax.swing.JTextField txtInleggeld;
    private javax.swing.JTextField txtMinAantalSpelers;
    private javax.swing.JTextField txtNaam;
    private javax.swing.JTextField txtPostcode;
    private javax.swing.JTextField txtTijd;
    // End of variables declaration//GEN-END:variables
}
